package com.googlecode.lookie.core.log;

import java.util.HashSet;
import java.util.Set;

public abstract class AbstractLog
    implements Log
{

    private Set<LogListener> logListeners = new HashSet<LogListener>( 5 );

    protected Set<LogListener> getLogListeners()
    {
        return logListeners;
    }

    public void addLogListener( LogListener listener )
    {
        synchronized ( logListeners )
        {
            logListeners.add( listener );
        }
    }

    public void removeLogListener( LogListener listener )
    {
        synchronized ( logListeners )
        {
            logListeners.remove( listener );
        }
    }

    protected void notifyLogging(LogRecord record) {
        LogListener[] listeners = null;
        synchronized ( logListeners )
        {
            listeners = (LogListener[]) logListeners.toArray( new LogListener[logListeners.size()] );
        }
        for ( LogListener listener : listeners )
        {
            notifyLogging( listener, record );
        }
    }

    protected void notifyLogging(LogListener listener, LogRecord record) {
        listener.logging(  record, this );
    }

}
